-
Notifications
You must be signed in to change notification settings - Fork 280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce injected session
util in user-code
#214
Conversation
@@ -932,9 +932,8 @@ export async function getCustomerOrder( | |||
} | |||
|
|||
export async function getCustomer( | |||
context: HydrogenContext, | |||
context: AppLoadContext & HydrogenContext, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is kinda funky: Because we define storefront
etc in HydrogenContext
from the package, and then augment AppLoadContext
in local types, these two sets need to be merged 🤔 Any better way to do this?
Re: "user land" comment … is this one of those things we can provide a nice default for in a Would be nice if you didn't have to copy/paste from the demo store. Or otherwise we could include it in the |
@@ -57,7 +53,7 @@ export const action: ActionFunction = async ({request, context, params}) => { | |||
|
|||
return redirect(params.lang ? `${params.lang}/account` : '/account', { | |||
headers: { | |||
'Set-Cookie': await session.commit(), | |||
'Set-Cookie': await sessionStorage.commitSession(session), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels weird - not exactly sure what I am committing to the session
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I follow. You're committing the session object, which was modified above this line, yeah? This is part of the official Remix API: https://remix.run/docs/en/v1/api/remix#sessions
Overall, I like this - This bounds a session to a worker request. One thing I cannot tell is - would this affect server edge caching? |
const session = await getSession(request, context); | ||
const cartId = await session.get('cartId'); | ||
export async function loader({context, params}: LoaderArgs) { | ||
const cartId = await context.session.get('cartId'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️ this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the session property be directly on context
, or on context.storefront
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO this is unrelated to other storefront
things and wouldn't be included (similar to fetch
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree session is unrelated, but maybe a use-case for an API that is like storefront.getCartId()
, storefront.getAccessToken()
.
@@ -133,7 +130,7 @@ async function action({request, context, params}: ActionArgs) { | |||
|
|||
// cart created - we only need a Set-Cookie header if we're creating | |||
session.set('cartId', cart.id); | |||
headers.set('Set-Cookie', await session.commit()); | |||
headers.set('Set-Cookie', await sessionStorage.commitSession(session)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find this a bit confusing that we read from session
but we commit via sessionStorage
?
Also the commit feels a little verbose. Could we not have a cleaner commit()
that pre-applies the session
internally? So we can do something like:
headers.set('Set-Cookie', await sessionStorage.commit());
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could, but it would vary ever so slightly from the official Remix APIs that IMO it would confuse people: https://remix.run/docs/en/v1/api/remix#sessions
👋 @mjackson care to share any context about how this was designed?
storefront: { | ||
publicStorefrontToken: '3b580e70970c4528da70c98e097c2fa0', | ||
storeDomain: 'hydrogen-preview', | ||
storefrontApiVersion: '2022-10', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will oxygen.ts
be exposed to the user?
Are we not using the s2s token in h2?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will oxygen.ts be exposed to the user?
IMO we should, yes, as it provides an escape hatch into the Request lifecycle and allows devs to inject custom loader context.
Are we not using the s2s token in h2?
Not yet. We need to inject it by reading from the expected env var 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the tokens/options can be passed in env vars, maybe we should place oxygen.ts
in .hydrogen
directory?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question. I think we'll see oxygen.ts
customized quite a lot, with different env vars or injections happening in the Remix loader on a per-project basis. So that might dictate whether we move it to a "shadow" folder like .hydrogen
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏼
@benjaminsehl Yeah that's def a possibility! I still need to read up on the |
@wizardlyhel it shouldn't affect it more than it is today: the |
session
and sessionStorage
utils in user-codesession
util in user-code
Update: Thanks for the feedback. I agree that it is awkward to pass both I've pushed an alternate approach which defines a bespoke For example, you could imagine them adding a |
Amazing stuff. Yes, having the option to define session getters would be super useful for accounts! I had a thread saved were @jacob-ebey recommended it. |
} | ||
|
||
commit() { | ||
return this.sessionStorage.commitSession(this.session); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏼
Inspired by @jacob-ebey's repo here: https://github.com/jacob-ebey/remix-dashboard-d1/blob/main/worker.ts
This PR introduces a
session
context value which replaces the cumbersomegetSession(request, context)
we had scattered everywhere before.The reason we had to call
getSession()
in each loader individually was because working with environment variables in Worker runtimes is a drag (psst, WinterCG). They have to be injected into loadercontext.env
rather than available globally withprocess.env
.This PR aims to make this logic completely customizable by the developer, and, as such:
requestHandler
so it can accept any third-party context variables a developer might want to inject